package com.example.module_word.search.pages

import androidx.activity.compose.BackHandler
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.statusBarsPadding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.lifecycle.viewmodel.compose.viewModel
import com.example.module_word.R
import com.example.module_word.search.ui.SearchBar
import com.example.module_word.search.ui.SearchHistoryView
import com.example.module_word.search.viewmodel.DictionariesViewMode
import com.example.module_word.widget.noRippleClickable
import com.fenghuajueli.libbasecoreui.baseswitch.PublicFunction
import com.fenghuajueli.libbasecoreui.utils.BannerAdView

@Composable
fun DictionariesScreen(onClick: () -> Unit) {
    val vm = viewModel<DictionariesViewMode>()

    BackHandler {
        if (vm.word.value.isNotEmpty()) vm.wordChange("") else onClick.invoke()
    }
    Column(
        Modifier
            .fillMaxSize()
            .background(Color(0xFFF5F7FF))
            .statusBarsPadding()
    ) {

        Image(
            painter = painterResource(id = R.mipmap.icon_return),
            contentDescription = null,
            modifier = Modifier
                .noRippleClickable {
                    onClick.invoke()
                }
                .padding(start = 20.dp, top = 10.dp)
        )

        SearchBar(
            modifier = Modifier.padding(top = 20.dp),
            value = vm.word.value,
            onValueChange = vm::wordChange,
            onSearch = {
                if (!PublicFunction.checkCanUsedByPosition(1, true)) {
                    return@SearchBar
                }
                vm.getDictionaryResult()
            },
        )

        if (vm.word.value.isEmpty()) {
            SearchHistoryView(vm = vm)
        } else {
            ParaphraseView(vm = vm)
        }

        Spacer(modifier = Modifier.weight(1f))
        BannerAdView(tag = "DictionariesScreen")
    }
}

@Composable
private fun ParaphraseView(vm: DictionariesViewMode) {
    val dictionaryResult by vm.dictionaryResult.collectAsStateWithLifecycle()
    dictionaryResult?.let {
        Column(
            Modifier
                .fillMaxWidth()
                .padding(top = 17.dp, start = 20.dp, end = 20.dp)
                .clip(RoundedCornerShape(16.dp))
                .background(Color.White)
                .padding(20.dp)
        ) {
            Text(
                text = it.word,
                fontSize = 16.sp,
                fontWeight = FontWeight.Black,
                color = Color.Black
            )
            Spacer(modifier = Modifier.height(3.dp))
            Text(
                text = it.content,
                fontSize = 16.sp,
                color = Color(0x80333333),
            )
        }
    }
}